package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.order.mapper.PaymentMapper;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.order.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * @author 杨林
 * @create 2022-11-18 20:56 星期五
 * description:
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper,PaymentInfo> implements PaymentService {

    @Override
    public void saveOrderInfo(OrderInfo orderInfo, Integer paymentType) {

        //每个订单只能有一个交易记录
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderInfo.getId());
        queryWrapper.eq("payment_type", paymentType);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {  //代表订单已经存在
            return;
        }

        //创建交易记录
        PaymentInfo paymentInfo = new PaymentInfo();

        //属性赋值
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setUpdateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        Integer status = PaymentStatusEnum.UNPAID.getStatus();
        paymentInfo.setPaymentStatus(status);
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        //paymentInfo.setTradeNo();  //交易编号
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        //paymentInfo.setTradeNo(orderInfo.getOutTradeNo());
        //2022-10-20|北京协和医院|精神病专科|教授
        String subject =
                new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")
                        + "|" + orderInfo.getHosname()
                        + "|" + orderInfo.getDepname()
                        + "|" + orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        baseMapper.insert(paymentInfo);
    }


    /**
     * 获取支付记录
     * @param orderId
     * @param paymentType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {

        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(paymentType)){
            throw new YyghException(20001,"订单参数有误");
        }

        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.eq("payment_type", paymentType);

        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);

        return paymentInfo;
    }


    @Autowired
    private OrderService orderService;

    /**
     * 更新订单和支付记录的状态
     * @param out_trade_no
     * @param resultMap
     */
    @Override
    public void paySuccess(String out_trade_no,  Map<String, String> resultMap) {
        //根据out_trade_no查询订单
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no", out_trade_no);
        OrderInfo orderInfo = orderService.getOne(queryWrapper);

        //修改订单状态 （已支付）
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderService.updateById(orderInfo);

        //根据out_trade_no查询支付记录
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", out_trade_no);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);

        //修改支付状态  添加微信流水号
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        String transaction_id = (String)resultMap.get("transaction_id");
        paymentInfo.setTradeNo(transaction_id);
        //paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(resultMap.toString());

        baseMapper.updateById(paymentInfo);
    }
}